package com.xxgc.springboot.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: springboot
 * @description:
 * @author: Mr.abel(ShiJiaYi)
 * @create: 2022-09-08 11:09
 **/
@Configuration
@EnableSwagger2
@EnableKnife4j
public class Swagger2Config{

    @Bean
    public Docket createRestApi(Environment environment) {
        //设置只在开发中环境中启动swagger
        Profiles profiles=Profiles.of("dev");
        //表示如果现在是dev环境，则返回true 开启swagger
        boolean flag=environment.acceptsProfiles(profiles);
        /*添加接口请求头参数配置 没有的话 可以忽略*/
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        /**
         * 个人开发可以添加 多人开发有切换不了账号的BUG !!!!!!
         */
        /*tokenPar.name("token")
                .description("令牌")
                .defaultValue("123456")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();
        pars.add(tokenPar.build());*/

        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(pars)
                .apiInfo(apiInfo())
                //所在分组
                .groupName("springboot")
                //是否启动swagger 默认启动
                .enable(flag)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xxgc.springboot.controller")) //指定扫描包
                .paths(PathSelectors.any())
                .build();
    }


    /**
     * 配置ApiInfo信息
     * @return
     */
    private ApiInfo apiInfo() {

        //作者信息
        Contact author = new Contact("abel", "https://www.shijiayi.top", "111@qq.com");


        return new ApiInfo(
                "SpringBoot学习",
                "spingboot描述",
                "1.0",
                "urn:tos",
                author,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList()
        );

    }
}
